<!--
 * @Description: 
 * @Author: Liu Kunpeng
 * @Date: 2021-08-18 15:52:15
-->
<template>
<body>
    <div class="login_container">
        <!-- 登录框 -->
        <div class="left" @click="resetpage()">
        <div v-show="!leftshow">
            <img src="../assets/images/bg1-l.png" alt="四维步态" class="bg1_left" @click="leftlogin()">
        </div>
            <div class="loginForm1" v-show="leftshow">
                <el-form ref="loginFormRef1" :rules="loginRules" :model="loginForm1" class="login_form1" label-width="60px">
                    <!-- 用户名 -->
                    <el-form-item prop="username" label="账户">
                        <el-input v-model="loginForm1.username" placeholder="请输入用户名" prefix-icon="el-icon-user" clearable></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password" label="密码">
                        <el-input v-model="loginForm1.password" placeholder="请输入密码" clearable
                        prefix-icon="el-icon-thumb" type="password" @keyup.enter.native="login1()"></el-input>
                    </el-form-item>
                    <!-- 按钮 -->
                    <el-form-item>
                        <el-checkbox v-model="loginForm1.checked" fill="#6AF699">记住密码</el-checkbox>
                    </el-form-item>
                    <el-button type ="success" @click="login1()" class="btns1">登录</el-button>
                    <el-button type="info" @click="resetLoginForm1()">重置</el-button>
                </el-form>
            </div>
            
        </div>
        <div class="right" @click="resetpage()">
        <div v-show="!rightshow" >
            <img src="../assets/images/bg1-r.png" alt="3D扫描" class="bg1_right" @click="rightlogin()">
        </div>
            <div class="loginForm2"  v-show="rightshow" >
                <el-form ref="loginFormRef2" :rules="loginRules" :model="loginForm2" class="login_form2"  label-width="60px">
                    <!-- 用户名 -->
                    <el-form-item prop="username" label="账户">
                        <el-input v-model="loginForm2.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
                    </el-form-item>
                    <!-- 密码 -->
                    <el-form-item prop="password" label="密码">
                        <el-input v-model="loginForm2.password" placeholder="请输入密码"  
                        prefix-icon="el-icon-thumb" type="password" @keyup.enter.native="login2()"></el-input>
                    </el-form-item>
                    <!-- 按钮 -->
                    
                    <el-form-item >
                        <el-checkbox v-model="loginForm2.checked">记住密码</el-checkbox>
                    </el-form-item>
                        <el-button type ="success" @click="login2()" class="btns1">登录</el-button>
                        <el-button type="info" @click="resetLoginForm2()">重置</el-button>
                </el-form>
            </div>
        </div>
    </div>
</body>
</template>
<script>
import { setToken ,setType} from '@/utils/auth'
import { loginApi } from '@/api/index'
import { resetRouter } from '@/router/index'
const Base64 = require('js-base64').Base64

export default {
    data(){
        return{
            
            leftshow:false,
            rightshow:false,
            //表单数据对象
            loginForm1:{
                username:"",
                password:"",
                note: "0",
                type:"步态分析",
                checked: true,
            },
            loginForm2:{
                username:"",
                password:"",
                note: "0",
                type:"三维扫描",
                checked: true,
            },
            //验证对象
            loginRules:{
                //校验用户名
                username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },//必填项校验
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }//长度校验
                ],
                //校验密码
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },//必填项校验
                    { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }//长度校验
                ],
            },
        };
    },
    methods: {
        resetRouters(){
            resetRouter()
        },
        resetpage(){
            // this.leftshow = false
            // this.rightshow = false
        },
        //重置表单内容
        resetLoginForm1(){
            this.loginForm1.username = ""
            this.loginForm1.password = ""
        },
        resetLoginForm2(){
            this.loginForm2.username = ""
            this.loginForm2.password = ""
        },
        leftlogin(){
            this.leftshow = true
            this.rightshow = false

        },
        rightlogin(){
            this.leftshow = false
            this.rightshow = true

        },
        login1(){
            //1.验证校验规则
            this.$refs.loginFormRef1.validate(valid =>{
                if( !valid ) return; 
                loginApi(this.loginForm1).then(async res=>{
                    let token = res.data.token
                    let type = res.data.type
                    this.setUserInfo()
                    await setToken(token)
                    await setType(type)

                    this.$message.success("登陆成功")//信息提示
                    this.$router.push({path:"/home"})//页面跳转
                    window.sessionStorage.setItem("username",this.loginForm1.username)
                })
            })
        },
        login2(){
            //1.验证校验规则
            this.$refs.loginFormRef2.validate(valid =>{
                if( !valid ) return; 
                loginApi(this.loginForm2).then(async res=>{
                    let token = res.data.token
                    let type = res.data.type
                    this.setUserInfo()
                    await setToken(token)
                    await setType(type)
                    this.$message.success("登陆成功")//信息提示
                    this.$router.push({path:"/home"})//页面跳转
                    window.sessionStorage.setItem("username",this.loginForm2.username)
                })
            })
        },
        setUserInfo() {
            // 判断用户是否勾选记住密码，如果勾选，向cookie中储存登录信息，
            // 如果没有勾选，储存的信息为空
            if(this.loginForm1.checked){
                this.setCookie("account1",this.loginForm1.username)
                // base64加密密码
                let passWord1 = Base64.encode(this.loginForm1.password)
                this.setCookie("password1",passWord1)    
            }else{
                this.setCookie("account1","")
                this.setCookie("password1","") 
            } 
            if(this.loginForm2.checked){
                this.setCookie("account2",this.loginForm2.username)
                // base64加密密码
                let passWord2 = Base64.encode(this.loginForm2.password)
                this.setCookie("password2",passWord2)    
            }else{
                this.setCookie("account2","")
                this.setCookie("password2","") 
            }
        },
        setCookie(cName, value, expiredays) {
            var exdate = new Date()
            exdate.setDate(exdate.getDate() + expiredays)
            document.cookie = cName + '=' + decodeURIComponent(value) +
            ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString())
        },
        getCookie(key) {
            if (document.cookie.length > 0) {
            var start = document.cookie.indexOf(key + '=')
            if (start !== -1) {
                start = start + key.length + 1
                var end = document.cookie.indexOf(';', start)
                if (end === -1) end = document.cookie.length
                return unescape(document.cookie.substring(start, end))
            }
            }
            return ''
        },
    },
    created() {
        this.resetRouters()
        // 在页面加载时从cookie获取登录信息
        let account1 = this.getCookie("account1")
        let password1 = Base64.decode(this.getCookie("password1"))
        // 如果存在赋值给表单，并且将记住密码勾选
        if(account1){
            this.loginForm1.username = account1
            this.loginForm1.password = password1
        }
        let account2 = this.getCookie("account2")
        let password2 = Base64.decode(this.getCookie("password2"))
        // 如果存在赋值给表单，并且将记住密码勾选
        if(account2){
            this.loginForm2.username = account2
            this.loginForm2.password = password2
        }
    },
}
</script>
<style lang="less" scoped>
// 根节点样式
.login_container{
    background: url(../assets/images/bg1.png);
    background-repeat: no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    height: 100%;

}

.left{
    
    float: left;
    width: 50%;
    height: 100%;
    position: relative;
    .loginForm1{
        width: 386px;
        height: 386px;
        background: rgba(255,255, 255, 0.5);
        -webkit-backdrop-filter: blur();    
        backdrop-filter: blur();
        border-radius: 8px;
        position: absolute;
        right: 20%;
        top: 25%;
        .login_form1{
            position: absolute;
            left: 8%;
            top: 20%;
            width: 80%;
        }
    }
    .bg1_left{
        cursor: pointer;
        width: 50%;
        position: absolute;
        left: 30%;
        top: 20%;
    }
}
.right{
    float: left;
    width: 49%;
    height: 100%;
    position: relative;
    .loginForm2{
        width: 386px;
        height: 386px;
        background: rgba(255,255, 255, 0.5);
        -webkit-backdrop-filter: blur();    
        backdrop-filter: blur();
        border-radius: 8px;
        position: absolute;
        left: 20%;
        top: 25%;
        
        .login_form2{
            position: absolute;
            left: 8%;
            top: 30%;
            width: 80%;
        }
    }
    .bg1_right{
        cursor: pointer;
        width: 50%;
        position: absolute;
        left: 20%;
        top: 20%;
    }
}
.btns1{
    float: right;
}
</style>
<style lang="scss">
.el-checkbox__input.is-checked .el-checkbox__inner,
.el-checkbox__input.is-indeterminate .el-checkbox__inner {
border-color: #409EFF !important;
background-color: #409EFF !important;
}
.el-checkbox__input.is-checked+.el-checkbox__label{
color: #050505 !important;
}
.el-checkbox__inner:hover{
border-color: #409EFF !important;
}
.el-checkbox__input.is-focus .el-checkbox__inner {
border-color: #409EFF !important;
}
</style>